﻿#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

template<class key,class value>
struct AVLTreeNode
{
	pair<key, value> _kv;

	AVLTreeNode<key, value>* _left;
	AVLTreeNode<key, value>* _right;
	AVLTreeNode<key, value>* _parent;
	int _bf;//平衡因子

	AVLTreeNode(const pair<key,value>&kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_bf(0)
	{}
};

template<class key, class value>
class AVLTree
{
	typedef AVLTreeNode<key, value> Node;
public:
	bool insert(const pair<key, value>& kv)
	{
		//为空树时，插入第一个树节点
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			/*else
			{
				cout << "相同不给进";
				return false;
			}*/
		}
		/*if (parent->_left == cur)
			parent->_left = new Node(kv);
		else
			parent->_right = new Node(kv);*/

		cur = new Node(kv);
		if (parent->_kv.first < kv.first)
			parent->_right = cur;
		else
			parent->_left = cur;
		cur->_parent = parent;

		//更新平衡因子
		while (parent)
		{
			if (cur == parent->_left)
				parent->_bf--;
			else if (cur == parent->_right)
				parent->_bf++;

			if (parent->_bf == 0)
				break;
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//旋转
				if (parent->_bf == 2 && cur->_bf == 1)
				{
					//左单旋
					RoteteL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)
				{
					//右单旋
					RoteteR(parent);
				}
				else if(parent->_bf ==2 && cur->_bf== -1 )
				{
					//右左旋
					RoteteRL(parent);
				}
				else if(parent->_bf==-2 && cur->_bf== 1 )
				{
					//左右旋
					RoteteLR(parent);
				}
				else
				{
					assert(false);
				}

				break;
			}
			else
			{
				assert(false);
			}
		}

		return true;
	}

	//右单旋
	void RoteteR(Node*parent)
	{
		Node* cur = parent->_left;
		Node* curR = cur->_right;

		Node* pparent = parent->_parent;

		cur->_right = parent;
		parent->_left = curR;

		if (curR)
			curR->_parent = parent;

		parent->_parent = cur;

		//还有如果刚开始的旋转的节点要区分：根节点和不是根节点的情况
		if (pparent == nullptr)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
				pparent->_left = cur;
			else
				pparent->_right = cur;

			cur->_parent = pparent;
		}

		//最后更新平衡因子
		// 更新平衡因子
		// 右单旋操作后，参与旋转的节点平衡因子都变为0
		parent->_bf = cur->_bf = 0;
	}

	//左单旋
	void RoteteL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* curL = cur->_left;

		Node* pparent = parent->_parent;

		cur->_left = parent;
		parent->_right = curL;

		if (curL)
			curL->_parent = parent;

		if (pparent == nullptr)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
				pparent->_left = cur;
			else
				pparent->_right = cur;

			cur->_parent = pparent;
		}


		//更新平衡因子
		cur->_bf = parent->_bf = 0;
	}
	//左右旋
	void RoteteLR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* curR = cur->_right;

		int bf = curR->_bf;

		RoteteL(parent->_left);
		RoteteR(parent);

		//更新平衡因子
		if (bf == -1)
		{
			parent->_bf = 1;
			cur->_bf = 0;
			curR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			cur->_bf = -1;
			curR->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			cur->_bf = 0;
			curR->_bf = 0;
		}
		else
		{
			assert(false);
		}

	}
	//右左旋
	void RoteteRL(Node*parent)
	{
		Node* cur = parent->_right;
		Node* curR = cur->_left;

		int bf = curR->_bf;

		RoteteR(parent->_right);
		RoteteL(parent);
		
		//更新平衡因子
		if (bf == -1)
		{
			parent->_bf = 0;
			cur->_bf = 1;
			curR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = -1;
			cur->_bf = 0;
			curR->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			cur->_bf = 0;
			curR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	//自己尝试一遍花了7分钟
	//void RoteteR(Node* parent)
	//{
	//	Node* cur = parent->_left;
	//	Node* curR = cur->_right;

	//	Node* pparent = parent->_parent;

	//	cur->_right = parent;
	//	parent->_left = curR;
	//	if (curR)
	//		curR->_parent = parent;

	//	parent->_parent = cur;

	//	if (pparent == nullptr)
	//	{
	//		//为root根时
	//		_root = cur;
	//		cur->_parent = nullptr;
	//	}
	//	else
	//	{
	//		if (pparent->_left == parent)
	//			pparent->_left = cur;
	//		else
	//			pparent->_right = cur;
	//		cur->_parent = pparent;
	//	}
	//}


	int Height()
	{
		return _Height(_root);
	}

	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	int Size()
	{
		return _Size(_root);
	}
private:
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}

	int _Size(Node* root)
	{
		if (root == nullptr)
			return 0;
		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	bool _IsBalanceTree(Node* root)
	{
		// 空树也是AVL树
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因⼦：即pRoot左右⼦树的⾼度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		// 如果计算出的平衡因⼦与pRoot的平衡因⼦不相等，或者
// pRoot平衡因⼦的绝对值超过1，则⼀定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异" << endl;
			return false;
		}
		if (root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}
		// pRoot的左和右如果都是AVL树，则该树⼀定是AVL树
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}


	void _InOrder(Node*root)
	{
		if (root == nullptr)
			return ;

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << ' ';
		_InOrder(root->_right);
	}

private:
	Node* _root=nullptr;
};



///////////////////////////////////////////////////////////
//关于左右旋的自己的一些猜想：
//左右旋
//Node* chanroot = cur->_right;
//Node* pparent = parent->_parent;
//if (parent == _root)
//{
//	_root = chanroot;
//	_root->_parent = nullptr;
//}
//else
//{
//	if (pparent->_left == parent)
//		pparent->_left = chanroot;
//	else
//		pparent->_right = chanroot;
//}
//
//
//cur->_right = chanroot->_left;
//if (chanroot->_left)
//chanroot->_left->_parent = cur;
//parent->_left = chanroot->_right;
//if (chanroot->_right)
//chanroot->_right->_parent = parent;

//chanroot->_left = cur;
//cur->_parent = chanroot;
//chanroot->_right = parent;
//parent->_parent = chanroot;
